package cn.samler.diandi.api.controller;

import cn.samler.diandi.api.entity.Post;
import cn.samler.diandi.api.entity.PostComment;
import cn.samler.diandi.api.model.io.Response;
import cn.samler.diandi.api.model.io.request.post.Nearby;
import cn.samler.diandi.api.model.io.request.post.PostCommentList;
import cn.samler.diandi.api.model.io.request.pub.Page;
import cn.samler.diandi.api.model.io.request.post.Publish;
import cn.samler.diandi.api.model.io.request.post.Search;
import cn.samler.diandi.api.service.PostService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("post")
public class PostController {
    private final PostService postService;

    public PostController(PostService postService) {
        this.postService = postService;
    }

    /**
     * 列出自己的post列表
     *
     * @param form 分页表单
     * @return Response
     */
    @PostMapping("list")
    public Response list(@RequestBody @Valid Page form) {
        return postService.list(form);
    }

    /**
     * 列出附近需求
     *
     * @param form role and range
     * @return Response
     */
    @PostMapping("nearby")
    public Response nearby(@RequestBody @Valid Nearby form) {
        return postService.nearby(form);
    }

    /**
     * 匹配搜索
     *
     * @param data 搜索表单
     * @return Response
     */
    @PostMapping("search")
    public Response search(@RequestBody @Valid Search data) {
        return postService.search(data);
    }

    /**
     * post详情
     *
     * @param id post id
     * @return Response
     */
    @GetMapping("detail/{id}")
    public Response detail(@PathVariable("id") Integer id) {
        return postService.findById(id);
    }

    /**
     * 发布新的post
     * @param form 表单
     * @return Response
     */
    @PostMapping("publish")
    public Response publish(@RequestBody @Valid Publish form) {
        return postService.publish(form);
    }

    /**
     * 更新post
     * <p>前端传post表单的时候，如果字段没有更新可以传null，以达到节省流量的目的。</p>
     *
     * @param form 更新表单
     * @return Response
     */
    @PostMapping("update")
    public Response update(@RequestBody @Valid Post form) {
        return postService.update(form);
    }

    /**
     * 删除post
     *
     * @param id post id
     * @return Response
     */
    @GetMapping("delete/{id}")
    public Response delete(@PathVariable("id") Integer id) {
        return postService.remove(id);
    }

    /**
     * 下架post
     *
     * @param id post id
     * @return Response
     */
    @GetMapping("hide/{id}")
    public Response hide(@PathVariable("id") Integer id) {
        return postService.display(id, false);
    }

    /**
     * 上架post
     *
     * @param id post id
     * @return Response
     */
    @GetMapping("show/{id}")
    public Response show(@PathVariable("id") Integer id) {
        return postService.display(id, true);
    }

    /**
     * 发表评论/回复
     *
     * @param form 数据表单
     * @return Response
     */
    @PostMapping("comment")
    public Response comment(@RequestBody @Valid PostComment form) {
        return postService.publishComment(form);
    }

    /**
     * 获取评论列表
     *
     * @param form 分页表单
     * @return Response
     */
    @PostMapping("comment/list")
    public Response commentList(@RequestBody @Valid PostCommentList form) {
        return postService.listComment(form);
    }

}
